package com.mf.all.controller.customer;


import com.mf.all.emuns.enumsPublic.EnumPublicEnableOrDisable;
import com.mf.all.entity.dto.DtoCustomerContacts;
import com.mf.all.entity.req.customer.ReqCustomerStatus;
import com.mf.all.entity.req.publicReq.updateStatus.ReqPublicIntIdUpdateStatus;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.service.ServiceCustomer;
import com.mf.all.utils.UtilController;
import com.mf.all.utils.UtilsEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoCustomerAddr;
import com.mf.all.entity.req.customer.ReqCustomerAddr;
import com.mf.all.service.ServiceCustomerAddr;
import com.mf.all.entity.req.search.customer.SearchReqCustomerAddr;

import java.util.Arrays;
import java.util.List;

/**
 * 客户地址
 *
 * @date 2025-04-02 22:36:47
 */
@RestController
@RequestMapping("customerAddr")
@Tag(name = "客户地址")
public class ControllerCustomerAddr {
    @Resource
    private ServiceCustomerAddr servicecustomerAddr;

    @Resource
    private ServiceCustomer serviceCustomer;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("customerAddr:search")
    public Result<Page<DtoCustomerAddr>> findPage(@RequestBody SearchReqCustomerAddr searchReqCustomerAddr) {
        return Result.success(servicecustomerAddr.findPageByParams(searchReqCustomerAddr));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("customerAddr:search")
    public Result<List<DtoCustomerAddr>> list(@RequestBody SearchReqCustomerAddr searchReqCustomerAddr) {
        return Result.success(servicecustomerAddr.findListByParams(searchReqCustomerAddr));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("customerAddr:save")
    @Transactional

    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqCustomerAddr reqCustomerAddr) {
        serviceCustomer.isExistById(reqCustomerAddr.getCustomerId());
        servicecustomerAddr.save(UtilController.setBo(reqCustomerAddr, DtoCustomerAddr.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("customerAddr:update")
    @Transactional
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqCustomerAddr reqCustomerAddr) {
        serviceCustomer.isExistById(reqCustomerAddr.getCustomerId());
        servicecustomerAddr.updateById(UtilController.setBo(reqCustomerAddr, DtoCustomerAddr.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    @SaCheckPermission("customerAddr:delete")
    public Result<Void> delete(@RequestBody Integer[] ids) {
        servicecustomerAddr.removeByIds(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "设置默认客户地址")
    @PostMapping("/updateDefaultAddr")
    @SaCheckPermission("customerAddr:update")
    public Result<Void> updateDefaultAddr(@RequestParam(value = "id") Integer id, @RequestParam(value = "customerId") Integer customerId) {
        servicecustomerAddr.updateDefaultAddr(customerId, id);
        return Result.success();
    }

    @Operation(summary = "修改状态")
    @PostMapping("/updateStatus")
    @SaCheckPermission("customerContacts:update")
    public Result<Void> updateStatus(@Validated @RequestBody ReqCustomerStatus updateStatus) {
        UtilsEnum.existsInEnum(EnumPublicEnableOrDisable.class, updateStatus.getStatus());
        servicecustomerAddr.updateById(UtilController.setBo(updateStatus, DtoCustomerAddr.class));
        return Result.success();
    }


}
